package org.example.springtrans.controller;

import org.example.springtrans.model.UserInfo;
import org.example.springtrans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/2
 * Time: 13:39
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public Boolean register(UserInfo userInfo){
        //开启事务
        TransactionStatus status = transactionManager.getTransaction(transactionDefinition);
        Integer result = userService.register(userInfo);
        //回滚事务
        //transactionManager.rollback(status);
        //提交事务
        transactionManager.commit(status);
        return result > 0;
    }
}
